// ==UserScript==
// @name            SidebarMod.uc.js
// @description     Firefox侧边栏增强
// @include         chrome://browser/content/browser.xul
// @charset         UTF-8
// @author          NightsoN
// @note            v20130526: 增加中键点击标题或菜单则在标签页打开 by ywzhaiqi
// @note            v20130520: 更改了图标获取顺序，现在的获取顺序 style > favicon > 第一个子图标 by ywzhaiqi
// @note            v20130428: mino fixed by lastdream2013, add useful site
// @version         0.5
// ==/UserScript==

(function () {

if (!document.getElementById('sidebar-box')) return;
if (!window.SidebarMod) {
	window.SidebarMod = {
		operaLikeToggler: true,//是否显示Opera风格屏幕边缘侧边栏开关条
		sitelist:[
			{
				name: 'firefox内部功能',
                style: 'list-style-image: url("chrome://browser/skin/places/bookmark.png");-moz-image-region: rect(0px, 48px, 16px, 32px);',
				childs: [
					{
						name: '书签',
						url: 'chrome://browser/content/bookmarks/bookmarksPanel.xul',
                        favicon: "chrome://browser/content/abouthome/bookmarks.png"
					},
					{
						name: '历史',
						url: 'chrome://browser/content/history/history-panel.xul',
                        favicon: 'chrome://browser/content/abouthome/history.png'
					},
					{
						name: '下载',
						url: 'chrome://mozapps/content/downloads/downloads.xul',
                        favicon: "chrome://browser/content/abouthome/downloads.png"
					},
                    {
                        name: '扩展',
                        url: 'chrome://mozapps/content/extensions/extensions.xul',
                        favicon: "chrome://browser/content/abouthome/addons.png"
                    },
                    {
                        name: 'Scrapbook',
                        url: 'chrome://scrapbook/content/scrapbook.xul',
                        favicon: 'chrome://scrapbook/skin/main_24.png'
                    },
                    {
                        name: 'Console',
                        url: 'chrome://global/content/console.xul',
                        style: 'list-style-image: url("chrome://global/skin/console/console-toolbar.png");-moz-image-region: rect(0px, 24px, 24px, 0px);'
                    },
                    {
                        name: 'Preferences',
                        url: "about:config",
                        favicon: ''
                    },
                    {
                        name: 'Stylish',
                        url: 'chrome://stylish/content/manage-standalone.xul?sidebar',
                        favicon: 'chrome://stylish/skin/16.png'
                    }
				]
			},
			{
				name: '常用站点',
				childs: [
                    {
                        name: '豆瓣',
                        url: 'http://m.douban.com/',
                        favicon: ''
                    },
                    {
                        name: '网易',
                        url: 'http://3g.163.com/touch/',
                        favicon: ''
                    },
                    {
                        name: '饭否',
                        url: 'http://m.fanfou.com/',
                        favicon: ''
                    },
                    {
                        name: '猫扑',
                        url: 'http://3g.mop.com/',
                        favicon: ''
                    },
                    {
                        name: '亦歌',
                        url: 'http://www.1g1g.com/',
                        favicon: ''
                    },
                    {
                        name: 'Twitter',
                        url: 'https://mobile.twitter.com/',
                        favicon: ''
                    },
                    {
                        name: '豆瓣电台',
                        url: 'http://douban.fm/partner/sidebar',
                        favicon: ''
                    },
                    {
                        name: '新浪微博',
                        url: 'http://m.weibo.cn/',
                        favicon: ''
                    },
                   {
                        name: '腾讯微博',
                        url: 'http://1.t.qq.com/',
                        favicon: ''
                    },
                    {
                        name: '网易微博',
                        url: 'http://3g.163.com/t/',
                        favicon: ''
                    },
                    {
                        name: '巴士电台',
                        url: 'http://bus.fm/',
                        favicon: ''
                    },
                    {
                        name: '虾米电台',
                        url: 'http://kuang.xiami.com/res/kuang/xiamikuang0709.swf/',
                        favicon: ''
                    },
                    {
                        name: '维基百科',
                        url: 'http://zh.m.wikipedia.org/',
                        favicon: ''
                    },
                    {
                        name: '百度百科',
                        url: 'http://wapbaike.baidu.com/',
                        favicon: ''
                    },
                    {
                        name: '互动百科',
                        url: 'http://3g.baike.com/',
                        favicon: ''
                    },
                    {
                        name: '糗事百科',
                        url: 'http://wap2.qiushibaike.com/',
                        favicon: ''
                    },
                    {
                        name: '百度贴吧',
                        url: 'http://wapp.baidu.com/',
                        favicon: ''
                    },
                    {
                        name: 'Facebook',
                        url: 'http://iphone.facebook.com/',
                        favicon: ''
                    },
                    {
                        name: 'Google阅读器',
                        url: 'https://www.google.com/reader/i/',
                        favicon: ''
                    },
				]
			},
			{
				name: '邮箱',
				childs: [
					{
						name: 'Gmail邮箱',
						url: 'https://mail.google.com/mail/x/1cj43rhn0qhbt/?ltmpl=ecobh&nui=5&btmpl=mobile&shva=1',
						favicon: ''
					},
					{
						name: 'hotmail邮箱',
						url: 'https://login.live.com/?pcexp=false',
						favicon: ''
					},
					{
						name: '网易邮箱',
						url: 'http://m.mail.163.com/',
						favicon: ''
					},
					{
						name: 'QQ邮箱',
						url: 'http://w.mail.qq.com/',
						favicon: ''
					},
					{
						name: '139邮箱',
						url: 'http://wapmail.10086.cn/',
						favicon: ''
					},
				]
			},
			{
				name: '翻译',
				childs: [
					{
						name: 'google翻译',
						url: 'http://translate.google.com/m/translate',
						favicon: ''
					},
					{
						name: '有道词典',
						url: 'http://dict.youdao.com/m',
						favicon: ''
					},
					{
						name: '海词词典',
						url: 'http://3g.dict.cn/',
						favicon: ''
					},
					{
						name: '在线字典',
						url: 'http://www.chazidian.com/m/',
						favicon: ''
					},
				]
			},
			{
                name: '实用站点',
                childs: [
                    {
                        name: '淘宝',
                        url: 'http://m.taobao.com',
                        favicon: ''
                    },
                    {
                        name: '卓越',
                        url: 'http://www.amazon.cn/gp/aw/',
                        favicon: ''
                    },
                    {
                        name: '京东',
                        url: 'http://m.jd.com/',
                        favicon: ''
                    },
                    {
                        name: '招行',
                        url: 'http://m.cmbchina.com/',
                        favicon: ''
                    },
                    {
                        name: '工行',
                        url: 'http://wap.icbc.com.cn/',
                        favicon: ''
                    },
                    {
                        name: '去哪儿',
                        url: 'http://m.qunar.com/',
                        favicon: ''
                    },
                    {
                        name: '阿里旺旺',
                        url: 'http://webwwtb.im.alisoft.com/wangwang',
                        favicon: ''
                    },
                    {
                        name: 'Web QQ',
                        url: 'http://web.qq.com/',
                        favicon: ''
                    },
                    {
                        name: '大众点评网',
                        url: 'http://m.dianping.com/guangzhou',
                        favicon: ''
                    },
                    {
                        name: '写字板工具',
                        url: 'http://www.yushufang.me/writer/',
                        favicon: ''
                    },
                ]
            },
            {
                name: '其他',
                childs: [
                    {
                        name: 'hao123',
                        url: 'http://m.hao123.com/',
                        favicon: ''
                    },
                    {
                        name: '天气预报',
                        url: 'http://wap.weather.com.cn/wap/weather/101280101.shtml',
                        favicon: ''
                    },
                    {
                        name: '百度地图',
                        url: 'http://wapmap.baidu.com/',
                        favicon: ''
                    },
                    {
                        name: '快递查询',
                        url: 'http://wap.kuaidi100.com/',
                        favicon: ''
                    },
                    {
                        name: '手机查询',
                        url: 'http://m.showji.com/',
                        favicon: ''
                    },
                    {
                        name: '火车票查询',
                        url: 'http://t.qunar.com',
                        favicon: ''
                    },
                    {
                        name: 'Wolfram|Alpha',
                        url: 'http://m.wolframalpha.com/',
                        favicon: ''
                    },
                ]
            },
		],

		makeButton: function (sitelist, parent) {
			var i,
				len = sitelist.length,
				item,
				btn,
				menu,
				menupopup,
				menuitem,
				frag = document.createDocumentFragment();
				insertpoint = document.querySelector('#sidebar-header .tabs-closebutton');
			for (i = 0; i < len; i++) {
				item = sitelist[i];
				if (item.childs) {
					if (!parent) {
						btn = frag.appendChild(document.createElement('toolbarbutton'));
						btn.setAttribute('tooltiptext', item.name);
						btn.setAttribute('type', 'menu');
						btn.setAttribute('style', getIconStyle(item));
						menupopup = btn.appendChild(document.createElement('menupopup'));
						SidebarMod.makeButton(item.childs, menupopup);
					} else {
						if (item === 'sep') {
							parent.appendChild(document.createElement('menuseparator'));
						} else {
							menu = parent.appendChild(document.createElement('menu'));
							menu.setAttribute('label', item.name);
							menu.setAttribute('class', 'menu-iconic');
							menu.setAttribute('style', getIconStyle(item));
							menupopup = menu.appendChild(document.createElement('menupopup'));
							SidebarMod.makeButton(item.childs, menupopup);
						}
					}
				} else if (parent) {
					if (item === 'sep') {
						parent.appendChild(document.createElement('menuseparator'));
					} else {
						menuitem = parent.appendChild(document.createElement('menuitem'));
						menuitem.setAttribute('label', item.name);
						menuitem.setAttribute('tooltiptext', item.name);
						menuitem.setAttribute('url', item.url);
						menuitem.setAttribute('class', 'menuitem-iconic');
						// menuitem.setAttribute('src', item.favicon);
                        menuitem.setAttribute('style', getIconStyle(item));
						menuitem.setAttribute('oncommand', 'openWebPanel(this.getAttribute("tooltiptext"), this.getAttribute("url"))');
                        menuitem.setAttribute('onclick', 'SidebarMod.itemClicked(event, this.getAttribute("url"));');
					}
				} else {
					btn = frag.appendChild(document.createElement('toolbarbutton'));
					btn.setAttribute('tooltiptext', item.name);
					btn.setAttribute('style', getIconStyle(item));
					btn.setAttribute('url', item.url);
					btn.setAttribute('onclick', 'openWebPanel(this.getAttribute("tooltiptext"), this.getAttribute("url"))');
				}
			}
			insertpoint.parentNode.insertBefore(frag, insertpoint);

            function getIconStyle(item){
                if(item.style){
                    return item.style;
                }else{
                    // 如果不存在则取第一个子条目的 favicon
                    if(!item.favicon){
                        return item.childs && getIconStyle(item.childs[0]);
                    }
                    return 'list-style-image: url("' + item.favicon + '")';
                }
            }
		},
		makeSplitter: function () {
			var sidebarBox = document.getElementById('sidebar-box'),
				splitter = sidebarBox.parentNode.insertBefore(document.createElement('splitter'), sidebarBox),
				sidebarBoxArrow;
			splitter.setAttribute('id', 'sidebar-box-splitter');
			splitter.setAttribute('onclick', 'toggleSidebar();');
			sidebarBoxArrow = splitter.appendChild(document.createElement('div'));
			sidebarBoxArrow.id = 'sidebar-box-arrow';
			sidebarBoxArrow.className = sidebarBox.hidden ? 'right' : '';
			//sidebarBoxArrow.className = sidebarBox.collapsed ? 'right' : '';
		},
		toggleSidebar: function (commandID, forceOpen) {
			var sidebarBox = document.getElementById("sidebar-box"),
				sidebar = document.getElementById("sidebar"),
				sidebarTitle = document.getElementById("sidebar-title"),
				sidebarSplitter = document.getElementById("sidebar-splitter"),
				sidebarBoxArrow = document.getElementById('sidebar-box-arrow'),
				lastcommand = commandID || sidebarBox.getAttribute('sidebarcommand') || sidebarBox.getAttribute('sidebarlastcommand') || 'viewBookmarksSidebar';

			//if (!commandID && sidebarBox.collapsed) {
			if (!commandID && sidebarBox.hidden) {
				if (sidebarBox.getAttribute('sidebarcommand') === '') {
					toggleSidebar(lastcommand, true);
					sidebarBox.setAttribute('sidebarlastcommand', lastcommand);
				} else {
					sidebarBox.hidden = false;
					sidebarSplitter.hidden = false;
					//setToolbarVisibility(sidebarSplitter, true);
					//setToolbarVisibility(sidebarBox, true);
					if (sidebarBoxArrow) sidebarBoxArrow.className = '';
				}
				return;
			}

			if (!commandID) commandID = sidebarBox.getAttribute("sidebarcommand");
			let sidebarBroadcaster = document.getElementById(commandID);

			if (sidebarBroadcaster.getAttribute("checked") == "true") {
				if (!forceOpen) {
					if (sidebarBox.getAttribute('sidebarcommand') !== 'viewWebPanelsSidebar') {
						sidebar.setAttribute("src", "about:blank");
						sidebar.docShell.createAboutBlankContentViewer(null);
						sidebarBox.setAttribute("sidebarcommand", "");
						sidebarTitle.value = "";
						sidebarBox.setAttribute('sidebarlastcommand', lastcommand);
					}
					sidebarBox.setAttribute("sidebarcommand", "");
					sidebarBox.setAttribute('sidebarlastcommand', lastcommand);
					sidebarBroadcaster.removeAttribute("checked");
					sidebarBox.hidden = true;
					sidebarSplitter.hidden = true;
					//setToolbarVisibility(sidebarSplitter, false);
					//setToolbarVisibility(sidebarBox, false);
					if (sidebarBoxArrow)sidebarBoxArrow.className = 'right';
					gBrowser.selectedBrowser.focus();
				} else {
					fireSidebarFocusedEvent();
				}
				return;
			}

			var broadcasters = document.getElementsByAttribute("group", "sidebar");
			for (let broadcaster of broadcasters) {
				if (broadcaster.localName != "broadcaster") continue;

				if (broadcaster != sidebarBroadcaster) broadcaster.removeAttribute("checked");
				else sidebarBroadcaster.setAttribute("checked", "true");
			}

			sidebarBox.hidden = false;
			sidebarSplitter.hidden = false;
			//setToolbarVisibility(sidebarSplitter, true);
			//setToolbarVisibility(sidebarBox, true);
			if (sidebarBoxArrow)sidebarBoxArrow.className = '';

			var url = sidebarBroadcaster.getAttribute("sidebarurl");
			var title = sidebarBroadcaster.getAttribute("sidebartitle");
			if (!title) title = sidebarBroadcaster.getAttribute("label");
			sidebar.setAttribute("src", url);
			sidebarBox.setAttribute("sidebarcommand", sidebarBroadcaster.id);
			if ( title &&  title !== '') sidebarTitle.value = title;
			sidebarBox.setAttribute("src", url);
			sidebarBox.setAttribute('sidebarlastcommand', lastcommand);

			if (sidebar.contentDocument.location.href != url) sidebar.addEventListener("load", sidebarOnLoad, true);
			else
			fireSidebarFocusedEvent();
		},
		modifySidebarClickBehaviour: function () {
			var sidebar = document.getElementById('sidebar');
			sidebar.addEventListener('DOMContentLoaded', function(){
				if (sidebar.contentDocument){
					sidebar.removeEventListener('DOMContentLoaded', arguments.callee, false);
					var wpb = sidebar.contentDocument.getElementById('web-panels-browser');
					if (wpb) {
						wpb.onclick = null;
					}
				}
			}, false);

			eval("window.asyncOpenWebPanel = " + window.asyncOpenWebPanel.toString().slice(0, -1) +
				'var wpb = sidebar.contentDocument.getElementById("web-panels-browser");' +
				'if (wpb) wpb.onclick = null;' + '}'
			);

			eval("window.openWebPanel = " + window.openWebPanel.toString().slice(0, -1) +
				'var wpb = sidebar.contentDocument.getElementById("web-panels-browser");' +
				'if (wpb) wpb.onclick = null;' + '}'
			);
		},
        itemClicked: function(e, url){
            if(e.button == 1){
                gBrowser.selectedTab = gBrowser.addTab(url);
            }
        },
        addEventListener: function(){
            var sidebarTitle = document.getElementById('sidebar-title');
            sidebarTitle.addEventListener("click", function(e){
                if(e.button == 1){
                    openSidebarURL();
                }
            }, false);

            function openSidebarURL(){
                var sidebar = document.getElementById('sidebar');
                var webPanel = sidebar.contentDocument.getElementById("web-panels-browser");
                var url;
                if(webPanel){
                    url = webPanel.contentDocument.URL;
                }else{
                    url = sidebar.getAttribute("src");
                }
                if(!url) return;
                gBrowser.selectedTab = gBrowser.addTab(url);
            }
        },
		init: function () {
			window.toggleSidebar = this.toggleSidebar;
			this.makeButton(this.sitelist);
			if (this.operaLikeToggler) {
				this.makeSplitter();
			}
			this.modifySidebarClickBehaviour();
			var css = ('\
			@-moz-document url(chrome://browser/content/browser.xul){\
				#sidebar-box-splitter {\
					background-color: #E8ECF6!important;\
					border-style: none!important;\
					cursor: default!important;\
					min-width: 0!important;\
					opacity: 0.8;\
				}\
				\
				#sidebar-box-splitter:hover {\
					background-color: #BFC4D2!important;\
				}\
				\
				#sidebar-box-arrow {\
					margin-top: -20px;\
				}\
				\
				#sidebar-box-arrow[class="right"] {\
					width: 0;\
					height: 0;\
					border-top: 4px solid transparent;\
					border-bottom: 4px solid transparent;\
					border-left: 2px solid #4B5660;\
				}\
				\
				#sidebar-box-arrow:not(.right) {\
					width: 0;\
					height: 0;\
					border-top: 4px solid transparent;\
					border-bottom: 4px solid transparent;\
					border-right: 2px solid #4B5660;\
				}\
			}\
			');
			var sss = Cc['@mozilla.org/content/style-sheet-service;1'].getService(Ci.nsIStyleSheetService);
			var uri = makeURI('data:text/css;charset=UTF=8,' + encodeURIComponent(css));
			if (!sss.sheetRegistered(uri, sss.AGENT_SHEET)) {
				sss.loadAndRegisterSheet(uri, sss.AGENT_SHEET);
			}

            this.addEventListener();
		}
	};

	SidebarMod.init();
}

})();
